package io.kiki.stack.netty.processor;

import io.kiki.stack.netty.channelManager.ChannelContext;
import io.kiki.stack.netty.command.Command;
import io.kiki.stack.netty.command.CommandFactory;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executor;


@Slf4j
@Setter
@Getter
public abstract class AbstractProcessor<T extends Command> implements Processor<T> {
    private Executor executor;
    private CommandFactory commandFactory;

    public AbstractProcessor() {

    }

    public AbstractProcessor(CommandFactory commandFactory) {
        this.commandFactory = commandFactory;
    }

    public AbstractProcessor(Executor executor) {
        this.executor = executor;
    }

    public AbstractProcessor(CommandFactory commandFactory, Executor executor) {
        this.commandFactory = commandFactory;
        this.executor = executor;
    }

    public abstract void doProcess(ChannelContext channelContext, T msg) throws Exception;


    @Override
    public void process(ChannelContext channelContext, T msg, Executor defaultExecutor) throws Exception {
        ProcessTask processTask = new ProcessTask(channelContext, msg, this);
        if (this.getExecutor() != null) {
            this.getExecutor().execute(processTask);
        } else {
            defaultExecutor.execute(processTask);
        }
    }

    @Override
    public Executor getExecutor() {
        return executor;
    }

    @Override
    public void setExecutor(Executor executor) {
        this.executor = executor;
    }


}
